/**
 * 点击切换手机注册或者邮箱注册
 */
+function(){
    let phone = $('.phoneReg'),
        email = $('.emailReg'),
        phoneShow = $('.phone'),
        emailShow = $('.email')
    $(phone).click(function(){
        $(phoneShow).css({
            display:'block'
        })
        $(emailShow).css({
            display:'none'
        })
        $(phone).toggleClass("active",true);
        $(email).toggleClass("active",false);
    });
    $(email).click(function(){
        $(phoneShow).css({
            display:'none'
        })
        $(emailShow).css({
            display:'block'
        })
        $(phone).toggleClass("active",false);
        $(email).toggleClass("active",true);
    })
}()
/**
 * 正则验证手机号码,邮箱地址,密码
 */
+function(){
    $(".phoneNum").change(function(){
        let phone =  $(".phoneNum").val();

        let regPhone=/^1\d{10}$/;    
        
        if(regPhone.test(phone)==false){
            $(".phoneNum").next().html("手机号码不正确，请重新输入");
            return false;
        }
        $(".phoneNum").next().html(" ");
        return true;
           
    })
    $(".emailAdr").change(function(){
        let email =  $(".emailAdr").val();

        let regEmail=/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;

        if(regEmail.test(email)==false){
            $(".emailAdr").next().html("Email格式不正确，例如web@sohu.com");
            return false;
        }
        $(".emailAdr").next().html(" ");
        return true;
    
    })
    $(".password").change(function(){
        let password = $(".password").val();
        let reg=/^[a-zA-Z0-9]{4,10}$/;                  //正则密码判断
        if(reg.test(password)==false){
            $(".password").next().html("密码不能含有非法字符，长度在4-10之间");
            return false;
        }else{
            $(".password").next().html(" ");
            return true;
        }
    }) 
}()
/**
 * 点击立即登录获取用户名和密码
 */
//引入xhrPost函数
function xhrPost(url,data){
    return new Promise((resolve,reject)=>{
          var xhr = new XMLHttpRequest();
          xhr.open("POST",url);
          xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          var _data = [];
          for(let key in data){
                _data.push(`${key}=${data[key]}`);
          }
          xhr.send(_data.join("&"));
          xhr.onload = function(){
                xhr.status === 200 ? resolve(xhr.responseText) : reject(xhr.status);
          }
    })
}
//点击立即登录获取用户名和密码
+function(){
    $(".registerNow").click(function(){
        let username = '';
        let password = '';

        let phoneNum = $(".phoneNum").val();
        let phonePass = $(".phonePass").val();

        let emailAdr = $(".emailAdr").val();
        let emailPass = $(".emailPass").val();

        if(!phoneNum&&!phonePass){
            username = emailAdr,
            password = emailPass
        }else{
            username = phoneNum,
            password = phonePass
        }
        xhrPost("http://120.79.139.196/antaPhp/register.php",{
            username : username,
            password : password
        }).then(function(res){
            res = JSON.parse(res);
            console.log(res)
            if(res.statu === "success"){
                setTimeout(function(){
                    location.href = "./login.html";
                },3000)
            }
        })
    })
}()